<template>
  <a-layout-header class="header" style="padding: 0 0px">
    <a-row>
      <a-col :span="3" >
        <div class="logo" />
      </a-col>
      <a-col :span="16">
        <a-menu
            v-model:selectedKeys="selectedKeys1"
            theme="dark"
            mode="horizontal"
            :style="{ lineHeight: '64px', lineWidth: '400px' }"
        >
          <a-menu-item key="/">
            <router-link to="/">首  页</router-link>
          </a-menu-item>
          <a-menu-item key="/admin/user" v-if="user.id">
            <router-link to="/admin/user">用户管理</router-link>
          </a-menu-item>
          <a-menu-item key="/admin/ebook" v-if="user.id">
            <router-link to="/admin/ebook">电子图书管理</router-link>
          </a-menu-item>
          <a-menu-item key="/admin/category" v-if="user.id">
            <router-link to="/admin/category">分类管理</router-link>
          </a-menu-item>
          <a-menu-item key="/about">
            <router-link to="/about">关于我们</router-link>
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col :span="5" style="text-align: center;color: white">
        <a style="color: white" @click="showLoginModal" v-show="!user.id">
          <span>登录</span>
        </a>
        <span v-show="user.id">您好：{{user.name}}</span>
        <a-popconfirm
            title="确认退出登录?"
            ok-text="是"
            cancel-text="否"
            @confirm="logout"
        >
          <a v-show="user.id">&nbsp;&nbsp;&nbsp;&nbsp;退出登录</a>
        </a-popconfirm>
      </a-col>
    </a-row>



    <a-modal
        title="登录表单"
        v-model:visible="loginModalVisible"
        :confirm-loading="loginModalLoading"
        @ok="login"
        ok-text="登录"
        cancel-text="取消"
    >
      <a-form :model="loginUser" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="登陆名">
          <!--    前端写的校验可以通过修改html给绕过，一定要在后端加上    -->
          <a-input v-model:value="loginUser.loginName"/>
        </a-form-item>
        <a-form-item label="密码" >
          <a-input v-model:value="loginUser.password" type="password"/>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-layout-header>
</template>
<script lang="ts">
import {computed, defineComponent, ref} from 'vue';
import axios from 'axios';
import { message } from 'ant-design-vue';
import {Tool} from "@/util/tool";
import store from "@/store";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'the-header',
  setup() {
    //登陆后保存用户信息
    const user = computed(() => {
      return store.state.user;
    });

    //登录时使用
    const loginUser = ref({
      loginName: "test",
      password: "test"
    });
    const loginModalVisible = ref(false);
    const loginModalLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    }

    //登录
    const reqUser = ref();
    const login = () => {
      loginModalLoading.value = true;
      //密码加密，不直接传user是因为避免新增的用户名已存在
      reqUser.value = {};
      reqUser.value = Tool.copy(loginUser.value);//不直接用user赋值因为传的是一个引用
      reqUser.value.password = hexMd5(reqUser.value.password + KEY);

      axios.post('/user/login', reqUser.value).then((response) => {
        loginModalLoading.value = false;

        const data = response.data;
        if (data.success) {
          loginModalVisible.value = false;

          // user.value = data.content;
          // loginUser.value.password = reqUser.value.password;

          store.commit("setUser", data.content);
          message.success(data.message);
          //暂时解决遗留BUG退出登录刷新页面，再登录不刷新user数据丢失刷新则恢复问题
          // window.location.reload();
        } else {
          message.error(data.message);
        }
      });
    }

    //退出登录
    const logout = () => {
      console.log("-------------------------------"+user.value.tok)
      axios.get('/user/logout/'+user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          store.commit("setUser", {});
          message.success(data.message);
        } else {
          message.error(data.message);
        }
      });
    }
    return {
      user,
      loginUser,
      showLoginModal,
      loginModalVisible,
      loginModalLoading,

      login,
      logout
    }
  }
});
</script>

<style>
  .logo{
    position: relative;
    margin: auto;
    width: 120px;
    height: 31px;
    background: rgba(255, 255, 255, 0.2);
    /*margin: 50% 50% 50% 50%;*/
    top: 16px;
    color: white;
    font-size: 18px;
  }
</style>